package com.wan.swaggerdemo.controller;

import com.wan.swaggerdemo.pojo.User;
import com.wan.swaggerdemo.pojo.UserParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.enums.ParameterStyle;
import io.swagger.v3.oas.annotations.headers.Header;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.io.FileUtils;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@Tag(name = "用户管理接口-POST请求")
@RestController
public class PostUserController {

    /**
     * 从header中获取参数
     *
     * @return
     */
    @Operation(summary = "从header中获取参数")
    @PostMapping("/getParamFromHeaderPost")
    public User getParamFromHeader(@Parameter(name = "name", description = "用户名", required = true,
            in = ParameterIn.HEADER, example = "zhangsan") @RequestHeader("name") String name) {
        User user = new User();
        user.setName(name);
        user.setPassword("123456");
        return user;
    }

    /**
     * POST单个或多个入参
     *
     * @return
     */
    @Operation(summary = "POST单个或多个入参", parameters = {
            @Parameter(name = "id", description = "主键", required = true, example = "1234567")
    })
    @PostMapping(value = "/getParamIdByParams")
    public String getParamIdByParams(@RequestParam(name = "id") Long id) {
        return "接收到的参数id：" + id;
    }

    /**
     * POST使用对象接收多个参数
     *
     * @return
     */
    @Operation(summary = "POST使用对象接收多个参数", parameters = {
            @Parameter(name = "name", description = "用户名", required = true, example = "zhangsan"),
            @Parameter(name = "password", description = "密码", required = false, example = "1111")
    })
    @PostMapping(value = "/getParamIdByUser")
    public User getParamIdByUser(@Parameter(hidden = true) @RequestBody User user) {
        return user;
    }

    /**
     * POST传参类型(单个或多个入参):application/json
     *
     * @return
     */
    @Operation(summary = "传参类型-application/json")
    @PostMapping(value = "/getParamByJson", consumes = MediaType.APPLICATION_JSON_VALUE)
    public String getParamByJson(@RequestBody User user) {
        return "接收到的参数name：" + user.getName() + "接收到的参数password：" + user.getPassword();
    }

    /**
     * POST传参类型(对象):application/json
     *
     * @return
     */
    @Operation(summary = "(对象)传参类型-application/json")
    @PostMapping(value = "/getParamByJsonUser", consumes = MediaType.APPLICATION_JSON_VALUE,
            headers = "content-type=application/json")
    public User getParamByJsonUser(@RequestBody User user) {
        return user;
    }

    /**
     * POST传参类型(对象):application/x-www-form-urlencoded
     *
     * @return
     */
    @Operation(summary = "传参类型-application/x-www-form-urlencoded")
    @PostMapping(value = "/getParamByxwfu", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public UserParam getParamByxwfu(@ParameterObject UserParam user) {
        return user;
    }

    /**
     * POST传参类型(对象):multipart/form-data
     * 注意：如果PostMapping中指定了请求参数的传递方式,方法参数如果使用@Requestbody注解时，Spring会自动将请求数据转陈application/json类型，
     * 如果与PostMapping中consumes指定的类型不一致，接口请求时就会报错 "Unsupported Media Type"，去掉@Requestbody注解即可。
     *
     * @return
     */
    @Operation(summary = "传参类型-multipart/form-data", method = "POST", parameters = {
            @Parameter(name = "name", description = "用户名", required = true, example = "zhangsan"),
            @Parameter(name = "password", description = "密码", required = false, example = "1111")
    }, requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(content = @Content(mediaType = "multipart/form-data")))
    @PostMapping(value = "/getParamByFormData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public User getParamByFormData(@Parameter(hidden = true,content = @Content(mediaType = "multipart/form-data")) User user) {
        return user;
    }

    /**
     * POST接收文件
     *
     * @return
     */
    @Operation(summary = "POST接收文件", parameters = {
            @Parameter(name = "file", description = "文件", required = true)
    })
    @PostMapping(value = "/uploadFile")
    public String uploadFile(MultipartFile file) throws IOException {
        File uploadFile = new File("D:\\softStore\\uploadFile\\backend", file.getOriginalFilename());
        // 判断配置的文件目录是否存在，若不存在则创建一个新的文件目录
        File parentFile = uploadFile.getParentFile();
        if (!parentFile.exists()) {
            parentFile.mkdirs();
        }
        file.transferTo(uploadFile);
        return "文件[" + file.getOriginalFilename() + "]上传成功";
    }


    /**
     * 12、POST接收base64文件上传
     *
     * @return
     */
    @Operation(summary = "POST接收base64文件")
    @PostMapping(value = "/uploadBase64File")
    public String uploadBase64File(@Parameter(name = "fileCoder", required = true, description = "base64文件编码") @RequestParam("fileCoder") String fileCoder) throws IOException {
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] bytes = decoder.decodeBuffer(fileCoder);
        InputStream in = new ByteArrayInputStream(bytes);

        String fileName = "文件-" + UUID.randomUUID().toString();
        File file = new File("D:\\softStore\\uploadFile\\backend", fileName);
        FileUtils.copyInputStreamToFile(in, file);
        return "文件[" + fileName + "]上传成功";
    }
}
